/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.vsu.cs.piit.vokod.TableModels;

import java.util.Calendar;
import java.util.Date;
import javax.swing.table.AbstractTableModel;
import ru.vsu.cs.piit.vokod.IListListener;
import ru.vsu.cs.piit.vokod.ObservableArrayList;
import ru.vsu.cs.piit.vokod.model.DailyMove;
import ru.vsu.cs.piit.vokod.model.Ward;

/**
 *
 * @author Галчонок
 */
public class MoveTableModel extends AbstractTableModel {

    ObservableArrayList<DailyMove> rows;
    private boolean EditMode = false;
    private boolean AddMode = false;
    DailyMove tmp;

    public MoveTableModel(ObservableArrayList<DailyMove> list) {
        rows = list;
        list.AddListener(new IListListener() {

            @Override
            public void doSave(Object object) {
                fireTableDataChanged();
            }

            @Override
            public void doRemove(Object object) {
                fireTableDataChanged();
            }
        });
    }

    @Override
    public int getRowCount() {
        if (AddMode)
            return rows.size()+1;
        return rows.size();
    }

    @Override
    public int getColumnCount() {
        return 16;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if (columnIndex==14)
            return false;
        if (EditMode)
            return true;
        if (AddMode)
            if (rowIndex==rows.size())
                return true;
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rowIndex<rows.size()) {
            switch (columnIndex)
            {
                case 0:
                    return rows.get(rowIndex).getDateOfMove();
                case 1:
                    return rows.get(rowIndex).getWard();
                case 2:
                    return rows.get(rowIndex).getMorningCount();
                case 3:
                    return rows.get(rowIndex).getEnterAll();
                case 4:
                    return rows.get(rowIndex).getEnterOutside();
                case 5:
                    return rows.get(rowIndex).getEnter018();
                case 6:
                    return rows.get(rowIndex).getEnterMore60();
                case 7:
                    return rows.get(rowIndex).getMoveFromOthers();
                case 8:
                    return rows.get(rowIndex).getMoveFromRao();
                case 9:
                    return rows.get(rowIndex).getMoveAtOthers();
                case 10:
                    return rows.get(rowIndex).getMoveAtRao();
                case 11:
                    return rows.get(rowIndex).getLeaveHome();
                case 12:
                    return rows.get(rowIndex).getLeaveAtOthers();
                case 13:
                    return rows.get(rowIndex).getDied();
                case 14:
                    return rows.get(rowIndex).getEveningCount();
                case 15:
                    return rows.get(rowIndex).getEveningCountOutside();
            }
        } else {
            switch (columnIndex)
            {
                case 0:
                    return tmp.getDateOfMove();
                case 1:
                    return tmp.getWard();
                case 2:
                    return tmp.getMorningCount();
                case 3:
                    return tmp.getEnterAll();
                case 4:
                    return tmp.getEnterOutside();
                case 5:
                    return tmp.getEnter018();
                case 6:
                    return tmp.getEnterMore60();
                case 7:
                    return tmp.getMoveFromOthers();
                case 8:
                    return tmp.getMoveFromRao();
                case 9:
                    return tmp.getMoveAtOthers();
                case 10:
                    return tmp.getMoveAtRao();
                case 11:
                    return tmp.getLeaveHome();
                case 12:
                    return tmp.getLeaveAtOthers();
                case 13:
                    return tmp.getDied();
                case 14:
                    return tmp.getEveningCount();
                case 15:
                    return tmp.getEveningCountOutside();
            }
        }
        return null;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex)
    {
        if (rowIndex<rows.size()) {
            switch (columnIndex)
            {
                case 0:
                    rows.get(rowIndex).setDateOfMove(((Calendar)aValue).getTime());
                    break;
                case 1:
                    rows.get(rowIndex).setWard((Ward)aValue);
                    break;
                case 2:
                    rows.get(rowIndex).setMorningCount((Integer)aValue);
                    break;
                case 3:
                    rows.get(rowIndex).setEnterAll((Integer)aValue);
                    break;
                case 4:
                    rows.get(rowIndex).setEnterOutside((Integer)aValue);
                    break;
                case 5:
                    rows.get(rowIndex).setEnter018((Integer)aValue);
                    break;
                case 6:
                    rows.get(rowIndex).setEnterMore60((Integer)aValue);
                    break;
                case 7:
                    rows.get(rowIndex).setMoveFromOthers((Integer)aValue);
                    break;
                case 8:
                    rows.get(rowIndex).setMoveFromRao((Integer)aValue);
                    break;
                case 9:
                    rows.get(rowIndex).setMoveAtOthers((Integer)aValue);
                    break;
                case 10:
                    rows.get(rowIndex).setMoveAtRao((Integer)aValue);
                    break;
                case 11:
                    rows.get(rowIndex).setLeaveHome((Integer)aValue);
                    break;
                case 12:
                    rows.get(rowIndex).setLeaveAtOthers((Integer)aValue);
                    break;
                case 13:
                    rows.get(rowIndex).setDied((Integer)aValue);
                    break;
                case 15:
                    rows.get(rowIndex).setEveningCountOutside((Integer)aValue);
                    break;
            }
            DailyMove m = rows.get(rowIndex);
            m.setEveningCount(m.getMorningCount()+
                              m.getEnterAll()+
                              m.getMoveFromOthers()+
                              m.getMoveFromRao()-
                              m.getMoveAtOthers()-
                              m.getMoveAtRao()-
                              m.getLeaveHome()-
                              m.getDied());
            rows.IsChanged(rows.get(rowIndex));
        } else {
            switch (columnIndex)
            {
                case 0:
                    tmp.setDateOfMove(((Calendar)aValue).getTime());
                    break;
                case 1:
                    tmp.setWard((Ward)aValue);
                    break;
                case 2:
                    tmp.setMorningCount((Integer)aValue);
                    break;
                case 3:
                    tmp.setEnterAll((Integer)aValue);
                    break;
                case 4:
                    tmp.setEnterOutside((Integer)aValue);
                    break;
                case 5:
                    tmp.setEnter018((Integer)aValue);
                    break;
                case 6:
                    tmp.setEnterMore60((Integer)aValue);
                    break;
                case 7:
                    tmp.setMoveFromOthers((Integer)aValue);
                    break;
                case 8:
                    tmp.setMoveFromRao((Integer)aValue);
                    break;
                case 9:
                    tmp.setMoveAtOthers((Integer)aValue);
                    break;
                case 10:
                    tmp.setMoveAtRao((Integer)aValue);
                    break;
                case 11:
                    tmp.setLeaveHome((Integer)aValue);
                    break;
                case 12:
                    tmp.setLeaveAtOthers((Integer)aValue);
                    break;
                case 13:
                    tmp.setDied((Integer)aValue);
                    break;
                case 15:
                    tmp.setEveningCountOutside((Integer)aValue);
                    break;
            }
            tmp.setEveningCount(tmp.getMorningCount()+
                              tmp.getEnterAll()+
                              tmp.getMoveFromOthers()+
                              tmp.getMoveFromRao()-
                              tmp.getMoveAtOthers()-
                              tmp.getMoveAtRao()-
                              tmp.getLeaveHome()-
                              tmp.getDied());
            fireTableRowsUpdated(rowIndex, rowIndex);
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
                switch (columnIndex)
        {
            case 0:
                return Date.class;
            case 1:
                return Ward.class;
            default:
                return Integer.class;
        }
    }



    @Override
    public String getColumnName(int column) {
        switch (column)
        {
            case 0:
                return "Дата";
            case 1:
                return "Отделение";
            case 2:
                return "<html>Состояло<br>больных";
            case 3:
                return "всего";
            case 4:
                return "сел. жит.";
            case 5:
                return "0-18 лет";
            case 6:
                return "<html>60 лет и<br>более";
            case 7:
                return "из др. отд.";
            case 8:
                return "из РАО";
            case 9:
                return "в др. отд.";
            case 10:
                return "в РАО";
            case 11:
                return "всего";
            case 12:
                return "в др. стац.";
            case 13:
                return "Умерло";
            case 14:
                return "всего";
            case 15:
                return "<html>в т.ч.<br>сел.жит.";
        }
        return String.valueOf(column);
    }

    /**
     * @return the EditMode
     */
    public boolean isEditMode() {
        return EditMode;
    }

    /**
     * @param EditMode the EditMode to set
     */
    public void setEditMode(boolean EditMode) {
        if (EditMode) this.AddMode = false;
        this.EditMode = EditMode;
        this.fireTableDataChanged();
    }

    /**
     * @return the AddMode
     */
    public boolean isAddMode() {
        return AddMode;
    }

    /**
     * @param AddMode the AddMode to set
     */
    public void setAddMode(boolean AddMode) {
        if (AddMode) {
            this.EditMode = false;
            this.tmp = new DailyMove(true);
            this.tmp.setDateOfMove(new Date());
        }
        this.AddMode = AddMode;
        this.fireTableDataChanged();
    }

    public boolean saveNewRow()
    {
        if (tmp.isCorrect())
        {
            rows.add(tmp);
            tmp = new DailyMove(true);
            return true;
        }
        return false;
    }

    public void delRow(int i)
    {
        rows.remove(rows.get(i));
    }

    public DailyMove getMove(int i)
    {
        return rows.get(i);
    }
}
